<?php
/**
 * Created by PhpStorm.
 * User: hoter.zhang
 * Date: 2016/3/22
 * Time: 17:41
 */

namespace app\core\components;


class Wechat {

    //GET参数
    public $signature;
    public $timestamp;
    public $nonce;
    public $echostr;
    public $token;

    public $postObj;
    //语音有关
    public $recognition;
    public $mediaId;

    //各类消息共有的属性
    public $fromUserName; //public 当openId使用
    private $toUserName;
    private $createTime;//消息创建时间 （整型）
    public  $msgType;//消息类型
    public  $content;//文本内容
    private $msgId;//消息id
    //图片
    private $picUrl;
    //语音
    private $format;
    //视频
    private $thumbMediaId;
    //地理位置
    private $location_X;
    private $location_Y;
    private $scale;
    private $label;
    //链接
    private $title;
    private $description;
    private $url;
    //事件推送
    public $event;

    public function __construct($params){//传递的$_GET参数，如果类中有则赋值
        foreach($params as $k => $v){
            if(property_exists($this, $k)) {
                $this->$k = $v;
            }
        }
    }

    // 验证是否通过
    public function valid() {
        if($this->checkSignature()) {
            echo $this->echostr;
            exit;
        }
    }

    // 验证服务器时效
    private function checkSignature() {
        $tmpArr = array($this->token, $this->timestamp, $this->nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        if( $tmpStr == $this->signature ){
            return true;
        }else{
            return false;
        }
    }

    public function initPostObj() {
        $postStr = empty($GLOBALS["HTTP_RAW_POST_DATA"]) ? '' : $GLOBALS["HTTP_RAW_POST_DATA"];
        if(!empty($postStr)) {
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $this->fromUserName = $postObj->FromUserName;
            $this->toUserName = $postObj->ToUserName;
            $this->msgType = $postObj->MsgType;
            $this->content = $postObj->Content;
            $this->event = $postObj->Event;
        } else {
            echo "this a file for weChat API!";
            exit;
        }
    }

    //向用户传回text消息的方法
    function transmitText($content) {
        $xmlTpl = "
            <xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[%s]]></Content>
            </xml>";
        return sprintf($xmlTpl, $this->fromUserName, $this->toUserName, time(), $content);
    }

    //向用户传回图文消息的方法
    function transmitNews($newsArray) {
        if(!is_array($newsArray)) {
            return '';
        }
        $itemTpl = "
            <item>
                <Title><![CDATA[%s]]></Title>
                <Description><![CDATA[%s]]></Description>
                <PicUrl><![CDATA[%s]]></PicUrl>
                <Url><![CDATA[%s]]></Url>
            </item>";
        $item_str = "";
        foreach ($newsArray as $item) {
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        }
        $xmlTpl ="
            <xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[news]]></MsgType>
                <ArticleCount>%s</ArticleCount>
                <Articles>
                    $item_str
                </Articles>
            </xml>";
        return sprintf($xmlTpl, $this->fromUserName, $this->toUserName, time(), count($newsArray));
    }

    //向用户传回音乐消息的方法
    function transmitMusic($music) {
        $xmlTpl ="
            <xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[music]]></MsgType>
                <Music>
                <Title><![CDATA[%s]]></Title>
                <Description><![CDATA[%s]]></Description>
                <MusicUrl><![CDATA[%s]]></MusicUrl>
                <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>

                </Music>
            </xml>";
        return sprintf($xmlTpl, $this->fromUserName, $this->toUserName, time(), $music['Title'],
            $music['Description'], $music['MusicUrl'], $music['HQMusicUrl']);
    }

    //向用户传回图片消息的方法
    function transmitImage($object, $content) {
        $xmlTpl = "
        <xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[image]]></MsgType>
            <Image>
            <MediaId><![CDATA[%s]]></MediaId>
            </Image>
        </xml>";
        return sprintf($xmlTpl,$this->fromUserName, $this->toUserName, time(),$content['MediaId']);
    }

    //向用户传回语音消息的方法
    function transmitVoice($content) {
        $xmlTpl = "
        <xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[voice]]></MsgType>
            <Voice>
            <MediaId><![CDATA[%s]]></MediaId>
            </Voice>
        </xml>";
        return sprintf($xmlTpl, $this->fromUserName, $this->toUserName, time(),$content['MediaId']);
    }

    //向用户传回视频消息的方法
    function transmitVideo($content){
        $xmlTpl = "
        <xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[video]]></MsgType>
            <Video>
            <MediaId><![CDATA[%s]]></MediaId>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            </Video>
        </xml>";
        return sprintf($xmlTpl,$this->fromUserName, $this->toUserName, time(),
            $content['MediaId'], $content['Title'], $content['Description']);
    }

}